﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class TinhThanhPhoManager
    {
        public bool Add(TinhThanhPho entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
            {
                CommandText = "spTinhThanhPhoInsert",
                CommandType = CommandType.StoredProcedure,
                Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTinhThanhPho",
                                                                                             entity.IDTinhThanhPho),
                                                                            new SqlParameter("@TenTinhThanhPho",
                                                                                             entity.TenTinhThanhPho)
                                                                        }
            });
        }

        public bool Update(TinhThanhPho entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spTinhThanhPhoUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTinhThanhPho",
                                                                                             entity.IDTinhThanhPho),
                                                                            new SqlParameter("@TenTinhThanhPho",
                                                                                             entity.TenTinhThanhPho)
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spTinhThanhPhoDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDTinhThanhPho", id)
                                                                        }
                                                    });
        }

        public List<TinhThanhPho> GetTinhThanhPho()
        {
            List<TinhThanhPho> tinhThanhPho = new List<TinhThanhPho>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from TinhThanhPho",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                tinhThanhPho.Add(TinhThanhPho.Convert(row));
            }
            return tinhThanhPho;
        }

        public TinhThanhPho GetTinhThanhPho(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from TinhThanhPho where IDTinhThanhPho = @IDTinhThanhPho",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDTinhThanhPho", id),
                                                                         }
                                                     });
            return TinhThanhPho.Convert(dtSet.Tables[0].Rows[0]);
        }
    }
}
